// Sales Order Template JavaScript
document.addEventListener('DOMContentLoaded', function() {
    // 初始化模板功能
    initTemplate();
    
    // 处理文件附件点击
    initFileAttachments();
    
    // 处理状态显示
    initStatusDisplay();
    
    // 处理自定义字段显示
    initCustomFields();
});

function initTemplate() {
    console.log('Sales Order Template initialized');
    
    // 添加打印功能
    addPrintButton();
    
    // 添加导出功能
    addExportButtons();
}

function initFileAttachments() {
    // 为所有文件附件添加点击事件
    const fileAttachments = document.querySelectorAll('.file-attachment');
    
    fileAttachments.forEach(function(attachment) {
        attachment.addEventListener('click', function(e) {
            e.preventDefault();
            const fileName = this.textContent;
            openFile(fileName);
        });
        
        // 添加悬停效果
        attachment.addEventListener('mouseenter', function() {
            this.style.backgroundColor = '#e9ecef';
        });
        
        attachment.addEventListener('mouseleave', function() {
            this.style.backgroundColor = 'white';
        });
    });
}

function initStatusDisplay() {
    // 为自定义状态添加动画效果
    const statusElements = document.querySelectorAll('.status-custom');
    
    statusElements.forEach(function(status) {
        // 添加淡入动画
        status.style.opacity = '0';
        status.style.transition = 'opacity 0.5s ease-in';
        
        setTimeout(function() {
            status.style.opacity = '1';
        }, 100);
    });
}

function initCustomFields() {
    // 为自定义字段添加交互效果
    const customFields = document.querySelectorAll('.custom-field-value');
    
    customFields.forEach(function(field) {
        // 添加悬停效果
        field.addEventListener('mouseenter', function() {
            this.style.backgroundColor = '#f8f9fa';
            this.style.borderColor = '#007bff';
        });
        
        field.addEventListener('mouseleave', function() {
            this.style.backgroundColor = 'white';
            this.style.borderColor = '#ddd';
        });
    });
}

function openFile(fileName) {
    // 模拟文件打开功能
    console.log('Opening file:', fileName);
    
    // 这里可以添加实际的文件打开逻辑
    // 例如：打开新窗口显示文件，或下载文件等
    alert('Opening file: ' + fileName);
}

function addPrintButton() {
    // 添加打印按钮
    const printButton = document.createElement('button');
    printButton.innerHTML = '🖨️ Print';
    printButton.className = 'print-button';
    printButton.style.cssText = `
        position: fixed;
        top: 20px;
        right: 20px;
        padding: 10px 20px;
        background-color: #007bff;
        color: white;
        border: none;
        border-radius: 5px;
        cursor: pointer;
        font-size: 14px;
        z-index: 1000;
    `;
    
    printButton.addEventListener('click', function() {
        window.print();
    });
    
    document.body.appendChild(printButton);
}

function addExportButtons() {
    // 添加导出按钮容器
    const exportContainer = document.createElement('div');
    exportContainer.className = 'export-container';
    exportContainer.style.cssText = `
        position: fixed;
        top: 60px;
        right: 20px;
        display: flex;
        flex-direction: column;
        gap: 10px;
        z-index: 1000;
    `;
    
    // PDF导出按钮
    const pdfButton = document.createElement('button');
    pdfButton.innerHTML = '📄 Export PDF';
    pdfButton.className = 'export-button';
    pdfButton.style.cssText = `
        padding: 8px 16px;
        background-color: #28a745;
        color: white;
        border: none;
        border-radius: 5px;
        cursor: pointer;
        font-size: 12px;
    `;
    
    pdfButton.addEventListener('click', function() {
        exportToPDF();
    });
    
    // Excel导出按钮
    const excelButton = document.createElement('button');
    excelButton.innerHTML = '📊 Export Excel';
    excelButton.className = 'export-button';
    excelButton.style.cssText = `
        padding: 8px 16px;
        background-color: #ffc107;
        color: #212529;
        border: none;
        border-radius: 5px;
        cursor: pointer;
        font-size: 12px;
    `;
    
    excelButton.addEventListener('click', function() {
        exportToExcel();
    });
    
    exportContainer.appendChild(pdfButton);
    exportContainer.appendChild(excelButton);
    document.body.appendChild(exportContainer);
}

function exportToPDF() {
    // 模拟PDF导出功能
    console.log('Exporting to PDF...');
    alert('PDF export functionality would be implemented here');
}

function exportToExcel() {
    // 模拟Excel导出功能
    console.log('Exporting to Excel...');
    alert('Excel export functionality would be implemented here');
}

// 工具函数
function formatCurrency(amount, currency = 'USD') {
    return new Intl.NumberFormat('en-US', {
        style: 'currency',
        currency: currency
    }).format(amount);
}

function formatDate(dateString) {
    if (!dateString) return 'N/A';
    
    const date = new Date(dateString);
    return date.toLocaleDateString('en-US', {
        year: 'numeric',
        month: 'short',
        day: 'numeric'
    });
}

// 打印样式
const printStyles = `
    @media print {
        .print-button,
        .export-container {
            display: none !important;
        }
        
        body {
            padding: 0 !important;
            margin: 0 !important;
        }
        
        .invoice-container {
            max-width: none !important;
            margin: 0 !important;
        }
        
        .custom-fields-section {
            page-break-inside: avoid;
        }
        
        .items-table {
            page-break-inside: avoid;
        }
    }
`;

// 添加打印样式到页面
const styleSheet = document.createElement('style');
styleSheet.textContent = printStyles;
document.head.appendChild(styleSheet);
